<?php

namespace app\admin\controller;

use think\Db;
use think\Cache;
use app\api\model\Task as TaskModel;
use app\api\model\Privatechat;

class Task extends Common
{
    public function index()
    {
        $where = ['channel'=>1];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $types = input('types');
        if ($types) {
            $where['types'] = $types;
        }

        $data = Db::name('task')->where($where)->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('types', $types);

        return $this->fetch();
    }

    public function add()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');
            $post['channel']=1;
            if (!$id) {
                $res = Db::name('task')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $res = Db::name('task')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
    
            Cache::rm('task');
        } else {
            if ($id) {
                $name = Db::name('task')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }

        return $data;
    }

    public function del()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('task')->delete($id);
    
                Cache::rm('task');
                
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
    public function AppScore()
    {
        $where = [];

        $status = input('status', 0);

        if ($status !== null) {
            $where['status'] = $status;
        }

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            if (in_array($search_type, ['id', 'yqr'])) {
                $where[$search_type] = $search_name;
            } else {
                $where[$search_type] = ['like', '%' . $search_name . '%'];
            }
        }

        $data = Db::name('task_for_app_score')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()])->each(function ($item, $key) {
            $user = Db::name('user')->find($item['user_id']);
            $item['user_name'] = $user ? $user['name'] : '';
            $item['images'] = explode(',', $item['images']);

            return $item;
        });

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('status', $status);

        return $this->fetch();
    }
    public function AppScoreShenhe()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');
            $task_for_app_score = db('task_for_app_score')->where('id', $id)->find();
            if ($task_for_app_score) {
                if ($task_for_app_score['status'] == 0) {
                    $db = db('task_for_app_score')->where('id', $id)->update(['status' => $post['shenhe'], 'reason'=> $post['reason'], 'update_time' => time()]);

                    if ($db) {
                        $content = 'APP评分任务结果：被驳回。驳回原因：' . $post['reason'];
                        if ($post['shenhe'] == 1) {
                            TaskModel::finish('app_score', $task_for_app_score['user_id']);
                            $content = '';
                        }
                        if ($content) {
                            // 通知用户
                            Privatechat::notice($task_for_app_score['user_id'], $content);
                        }
                        $data = ['ret' => '修改成功'];
                    } else {
                        $data = ['err' => '修改失败'];
                    }
                } else {
                    $data = ["err" => '当前状态不可变更'];
                }
            } else {
                $data = ["err" => '申请不存在'];
            }
        }
        return $data;
    }
    public function AppScoreDel()
    {
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
                $db = Db::name('task_for_app_score')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
    public function AppDownload()
    {
        $where = [];

        $status = input('status', 0);

        if ($status !== null) {
            $where['status'] = $status;
        }

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            if (in_array($search_type, ['id', 'yqr'])) {
                $where[$search_type] = $search_name;
            } else {
                $where[$search_type] = ['like', '%' . $search_name . '%'];
            }
        }
        $apps = Db::name('task_of_app_download')->column('name', 'id');
        $data = Db::name('task_of_app_download_records')->where($where)->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()])->each(function ($item, $key) use ($apps) {
            $user = Db::name('user')->find($item['user_id']);
            $item['user_name'] = $user ? $user['name'] : '';
            $item['user_sex'] = $user ? $user['sex'] : '';
            $item['images'] = explode(',', $item['images']);
            $item['app_name'] = $apps[$item['did']] ?? '';

            return $item;
        });

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        $this->assign('status', $status);

        return $this->fetch();
    }
    public function AppDownloadShenhe()
    {
        $id = input('id');

        if (request()->isAjax()) {

            $post = input('post.');
            $task_of_app_download_records = db('task_of_app_download_records')->where('id', $id)->find();
            if ($task_of_app_download_records) {
                if ($task_of_app_download_records['status'] == 0) {
                    $db = db('task_of_app_download_records')->where('id', $id)->update(['status' => $post['shenhe'], 'reason'=> $post['reason'], 'update_time' => time()]);

                    if ($db) {
                        $content = 'APP下载任务结果：被驳回。驳回原因：' . $post['reason'];
                        if ($post['shenhe'] == 1) {
                            $content = '';
                        }
                        if ($content) {
                            // 通知用户
                            Privatechat::notice($task_of_app_download_records['user_id'], $content);
                        }
                        $data = ['ret' => '修改成功'];
                    } else {
                        $data = ['err' => '修改失败'];
                    }
                } else {
                    $data = ["err" => '当前状态不可变更'];
                }
            } else {
                $data = ["err" => '申请不存在'];
            }
        }
        return $data;
    }
    public function AppDownloadDel()
    {
        if (request()->isAjax()) {

            $id = input('id');
            if ($id) {
                $db = Db::name('task_of_app_download_records')->delete($id);
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    public function dapps()
    {
        $where = [];

        $search_name = input('param.name');
        $search_type = input('param.search');
        if ($search_name && $search_type) {
            $where[$search_type] = ['like', '%' . $search_name . '%'];
        }

        $data = Db::name('task_of_app_download')->where($where)->paginate(_getCachePages(), false, ['query' => request()->param()]);

        $this->assign('data', $data);
        $page = $data->render();
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function adddapp()
    {
        $id = input("id");

        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {
                $res = Db::name('task_of_app_download')->insert($post);
                if ($res) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $res = Db::name('task_of_app_download')->where('id', $id)->update($post);
                if ($res) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
    
            Cache::rm('task');
        } else {
            if ($id) {
                $name = Db::name('task_of_app_download')->find($id);

                if ($name) {
                    $this->assign('name', $name);
                } else {
                    return ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }

        return $data;
    }

    public function deldapp()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('task_of_app_download')->delete($id);
    
                Cache::rm('task');
                
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
}
